import Vuex from 'vuex'

export const typeSource = 'https://seerh5.61.com/resource/assets/PetType/'
export const suitSource = 'https://seerh5.61.com/resource/assets/item/cloth/suiticon/'
export const petHeadSource = 'https://seerh5.61.com/resource/assets/pet/head/'
export const cosSource = 'https://xibie-1380171766.cos.ap-shanghai.myqcloud.com/xibie/'
export const cosSourcePetHead = 'https://xibie-1380171766.cos.ap-shanghai.myqcloud.com/xibie/petHead/'

export const rules = {
    'rule1': '双方指定第三轮使用的属性',
    'rule2': '双方开局各禁用对手一个套装',
    'rule3': '双方抽到的属性不会重复',
    'rule4': '双方开局各禁用一个属系',
    'rule5': '双方重抽和暗手次数变为各共计五次',
    'rule6': '双方只能使用衍生属性的精灵',
}

const store = new Vuex.Store({
    state: {
        loading: false,
        loadingDisable: false,
        eventSignal: {
            rollType: 0
        },
        user: {
            userid: '',
            nickname: ''
        },
        game: {
            // 非对称数据
            unilateral: {
                game_id: '',    //房间号
                side: '',   //当前选边：player1, player2
                count_down: 0,  //倒计时
                phase: {    //当前阶段
                    operation: 'wait',  //可执行操作：wait, readyStart, ban, pick
                    item: '',   //bp对象类型：game, suit, pet_type, pet
                    side: '',   //当前操作方：player1, player2
                    count: 1,   //记录在同样的operation&item&side组合下的第几次操作
                },
                rule_first: '', //规则一
                rule_second: '',    //规则二
                chance: {   //特殊操作（暗手，重随属系，皆可）剩余次数
                    hide: 0,    //暗手剩余次数
                    roll: 0,    //重随属系剩余次数
                    both: 0     //皆可剩余次数
                },
                pet_type: 0 //当前抽中的属系
            },
            // 对称数据
            symmetry: {
                player1: {  //玩家1（蓝方）数据
                    userid: '',  //玩家1id
                    name: '',    //玩家1昵称
                    ready: null,    //玩家1是否准备
                    suit_banned: null,  //玩家1禁用的套装
                    type_banned: null,  //玩家1禁用的属系
                    suit_picked: null,  //玩家1已选取的套装
                    chance: {   //玩家1特殊操作（暗手，重随属系，皆可）剩余次数
                        hide: 0,
                        roll: 0,
                        both: 0
                    },
                },
                player2: {  //玩家2（红方）数据
                    userid: '',  //玩家2id
                    name: '',    //玩家2昵称
                    ready: null,    //玩家2是否准备
                    suit_banned: null,  //玩家2禁用的套装
                    type_banned: null,  //玩家2禁用的属系
                    suit_picked: null,  //玩家2已选取的套装
                    chance: {   //玩家2特殊操作（暗手，重随属系，皆可）剩余次数
                        hide: 0,
                        roll: 0,
                        both: 0
                    },
                },
            },
            // 双方pick精灵情况，避免对称信息对象中塞太多信息单独提出来
            pets_picked: {
                player1: [{
                    pet_id: 1,  //精灵id
                    pet_origin_id: 1,   //精灵原始id
                    pet_head: '1.png',  //精灵头像文件名
                    hidden: false,  //是否是暗手
                    pet_type: 0,    //精灵属系
                    pet_name: ''
                }],
                player2: [{
                    pet_id: 1,
                    pet_origin_id: 1,
                    pet_head: '1.png',
                    hidden: false,
                    pet_type: 0,
                    pet_name: ''
                }]
            },
            hidden_open: 1, //是否对自己展示自己的暗手
            payload: {  //当前操作数据
                value: 0,   //选择的套装/精灵/属系id
                hidden: false,  //是否开启暗手选择
                pet_type_url: 0, //当前选择精灵时，需要额外带上原初id
            }
        }
    },
    getters: {
        /**
         * 获取当前阶段信息，并附上文字说明phase_text
         * @param state
         * @returns {{item: string, side: string, count: number, operation: string}}
         */
        getCurrentPhaseInfo: (state) => {
            let phase = state.game.unilateral.phase;
            if (state.game.unilateral.phase.operation)  {
                let phase_text = '';
                if (state.game.unilateral.phase.operation === 'wait') {
                    phase_text += '等待中';
                } else if (state.game.unilateral.phase.operation === 'readyStart') {
                    phase_text += '准备开始';
                } else {
                    switch (state.game.unilateral.phase.side) {
                        case 'player1': phase_text += '蓝方'; break;
                        case 'player2': phase_text += '红方'; break;
                        default: break;
                    }
                    switch (state.game.unilateral.phase.operation) {
                        case 'ban': phase_text += '禁用'; break;
                        case 'pick': phase_text += '选取'; break;
                        default: break;
                    }
                    switch (state.game.unilateral.phase.item) {
                        case 'suit': phase_text += '套装'; break;
                        case 'pet_type': phase_text += '属系'; break;
                        case 'pet': phase_text += '精灵'; break;
                        default: break;
                    }
                }
                Object.assign(phase, {
                    phase_text: phase_text
                })
            }
            return phase
        },
        getButtonState: (state) => {
            let buttonState = {
                text: 'verify',
                rollDisabled: false,
                disabled: false,
                loading: false
            }
            if (state.game.unilateral.phase.operation)  {
                if (state.game.unilateral.phase.operation === 'wait') {
                    buttonState.text = 'ready';
                    buttonState.rollDisabled = true
                    if (state.game.symmetry.player1.userid === state.user.userid) {
                        buttonState.disabled = state.game.symmetry.player1.ready
                    }
                    if (state.game.symmetry.player2.userid === state.user.userid) {
                        buttonState.disabled = state.game.symmetry.player2.ready
                    }
                } else if (state.game.unilateral.phase.operation === 'readyStart') {
                    buttonState.rollDisabled = true;
                    buttonState.disabled = true;
                } else {
                    if (state.game.unilateral.phase.side === 'player1' && state.game.symmetry.player1.userid !== state.user.userid) {
                        buttonState.rollDisabled = true
                        buttonState.disabled = true;
                    }
                    if (state.game.unilateral.phase.side === 'player2' && state.game.symmetry.player2.userid !== state.user.userid) {
                        buttonState.rollDisabled = true
                        buttonState.disabled = true;
                    }
                    if (state.game.unilateral.phase.item !== 'pet') {
                        buttonState.rollDisabled = true
                    }
                }
            } else {
                buttonState.rollDisabled = true
                buttonState.disabled = true;
            }
            return buttonState
        },
        /**
         * 获取当前抽中的属系的图片文件名
         * @param state
         * @returns {undefined|{pet_type: number, pet_type_url: string}}
         */
        getCurrentPetType: (state) => {
            if (state.game.unilateral.pet_type) {
                return {
                    pet_type: state.game.unilateral.pet_type,
                    pet_type_url: state.game.unilateral.pet_type === 8 ?
                        `${typeSource}prop.png`:`${typeSource}${state.game.unilateral.pet_type}.png`
                }
            }
            return undefined
        },
        /**
         * 获取当前对局的规则文本
         * @param state
         * @returns {{rule_first: string, rule_second: string}}
         */
        getRules: (state) => {
            let currRules = {
                rule_first: state.game.unilateral.rule_first,
                rule_second: state.game.unilateral.rule_second,
            }
            if (state.game.unilateral.rule_first && state.game.unilateral.rule_second) {
                Object.assign(currRules, {
                    rule_first_text: rules[state.game.unilateral.rule_first],
                    rule_second_text: rules[state.game.unilateral.rule_second]
                })
            }
            return currRules
        },
        /**
         * 当前对局的玩家1的信息
         * @param state
         * @returns {{chance: {hide: number, roll: number, both: number}, ready: boolean, type_banned: null, name: string, suit_picked: null, suit_banned: null}}
         */
        getPlayer1State: (state) => {
            let player1 = state.game.symmetry.player1
            if (state.game.symmetry.player1.suit_banned) {
                Object.assign(player1, {
                    suit_banned_url: `${suitSource}${state.game.symmetry.player1.suit_banned}.png`
                })
            }
            if (state.game.symmetry.player1.type_banned) {
                Object.assign(player1, {
                    type_banned_url: state.game.symmetry.player1.type_banned === 8?
                        `${typeSource}prop.png`:`${typeSource}${state.game.symmetry.player1.type_banned}.png`
            })
            }
            if (state.game.symmetry.player1.suit_picked) {
                Object.assign(player1, {
                    suit_picked_url: `${suitSource}${state.game.symmetry.player1.suit_picked}.png`
                })
            }
            console.log(player1)
            return player1
        },
        /**
         * 当前对局的玩家2的信息
         * @param state
         * @returns {{chance: {hide: number, roll: number, both: number}, ready: boolean, type_banned: null, name: string, suit_picked: null, suit_banned: null}}
         */
        getPlayer2State: (state) => {
            let player2 = state.game.symmetry.player2
            if (state.game.symmetry.player2.suit_banned) {
                player2 = Object.assign(player2, {
                    suit_banned_url: `${suitSource}${state.game.symmetry.player2.suit_banned}.png`
                })
            }
            if (state.game.symmetry.player2.type_banned) {
                player2 = Object.assign(player2, {
                    type_banned_url: state.game.symmetry.player2.type_banned === 8?
                        `${typeSource}prop.png`:`${typeSource}${state.game.symmetry.player2.type_banned}.png`
                })
            }
            if (state.game.symmetry.player2.suit_picked) {
                player2 = Object.assign(player2, {
                    suit_picked_url: `${suitSource}${state.game.symmetry.player2.suit_picked}.png`
                })
            }
            return player2
        },
        /**
         * 获取当前对局的玩家1的精灵选择
         * @param state
         * @returns {[{hidden: boolean, pet_head: string, pet_type: number, pet_origin_id: number, pet_id: number}]}
         */
        getPlayer1PetsPick: (state) => {
            let player1_pets_picked = state.game.pets_picked.player1
            player1_pets_picked.forEach(pick => {
                if (pick.pet_id) {
                    Object.assign(pick, {
                        pet_head_url: `${pick.pet_origin_id>4810 ? cosSourcePetHead : petHeadSource}${pick.pet_head}`
                    })
                }
                if (pick.pet_type) {
                    Object.assign(pick, {
                        pet_type_url: pick.pet_type === 8?
                            `${typeSource}prop.png`:`${typeSource}${pick.pet_type}.png`
                    })
                }
            })
            return player1_pets_picked
        },
        /**
         * 获取当前对局的玩家2的精灵选择
         * @param state
         * @returns {[{hidden: boolean, pet_head: string, pet_type: number, pet_origin_id: number, pet_id: number}]}
         */
        getPlayer2PetsPick: (state) => {
            let player2_pets_picked = state.game.pets_picked.player2
            player2_pets_picked.forEach(pick => {
                if (pick.pet_id) {
                    Object.assign(pick, {
                        pet_head_url: `${pick.pet_origin_id>4810 ? cosSourcePetHead : petHeadSource}${pick.pet_head}`
                    })
                }
                if (pick.pet_type) {
                    Object.assign(pick, {
                        pet_type_url: pick.pet_type === 8?
                            `${typeSource}prop.png`:`${typeSource}${pick.pet_type}.png`
                    })
                }
            })
            return player2_pets_picked
        }
    },
    mutations: {
        setLoading(state, payload) {
            if (state.loadingDisable){
                return
            }
            state.loading = payload
        },
        setLoadingDisable(state, payload) {
            state.loadingDisable = payload
        },
        setEventSignal(state) {
            state.eventSignal.rollType ++
        },
        setUserId(state, payload) {
            state.user.userid = payload
        },
        setUserNickname(state, payload) {
            state.user.nickname = payload
        },
        setGameStateUnilateral(state, payload) {
            state.game.unilateral = payload
        },
        setGameStateSymmetry(state, payload) {
            state.game.symmetry = payload
        },
        setGamePetsPick(state, payload) {
            state.game.pets_picked = payload
        },
        setItemSelected(state, payload) {
            state.game.payload.value = payload.value
            if (payload.pet_origin_id) state.game.payload.pet_origin_id = payload.pet_origin_id
        },
        changeHidden(state) {
            state.game.payload.hidden = !state.game.payload.hidden
        },
        setHidden(state, payload) {
            state.game.payload.hidden = payload
        },
        setHiddenOpen(state, payload) {
            state.game.hidden_open = payload
        },
    },
    actions: {
    }
})

export default store